package org.huangrui.algorithm;

/**
 * @Author hr
 * @Create 2025-11-03 11:24
 * 167. 两数之和 II - 输入有序数组
 * https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/
 **/
public class T021LeetCode167 {
    public int[] twoSum(int[] numbers, int target){
        // 一左一右两个指针相向而行
        int left = 0,right = numbers.length -1;
        while (left < right){
            int sum = numbers[left] + numbers[right];
            if (sum == target){
                // 题目要求:给你一个下标从 1 开始的整数数组 numbers...所以两个下标指针都加个1
                return new int[]{left+1,right+1};
            }else if (sum < target){
                left ++; //让sum大一点，左指针向右变大
            }else {
                right --; //让sum小一点，右指针向左变小
            }
        }
        return new int[]{-1,-1};
    }
    public static void main(String[] args) {
        T021LeetCode167 test = new T021LeetCode167();

        // int[] nums = new int[]{2,7,11,15,17};
        // int target = 18;
        // int target = 22;

        int[] nums = new int[]{2,3,4};
        int target = 6;
        int[] ints = test.twoSum(nums, target);

        for (int element : ints) {
            System.out.println(element);
        }

    }
}
